package org.example.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @link {https://zhuanlan.zhihu.com/p/91520481}
 * @link {https://blog.csdn.net/PlatoWG/article/details/108220764}
 * @link {https://docs.spring.io/spring-cloud-zookeeper/docs/2.2.5.RELEASE/reference/html/#spring-cloud-zookeeper-dependencies}
 * @link {https://blog.csdn.net/wangxi06/article/details/115459346}
 */
@RestController
public class ProductController {

    @Value("${person.name:没有从配置中心获取到值。}")
    private String name;

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private UserProperties userProperties;

    @GetMapping("/")
    public String home() {
        return "Hello World!";
    }

    @GetMapping("/service")
    public List service() {
        return serviceUrl();
    }

    @GetMapping("/product")
    public String getProducer() {
        return "来自producer服务的消息";
    }


    public List<ServiceInstance> serviceUrl() {
        return discoveryClient.getInstances("product-service");
    }


    /**
     * 测试从配置中心获取值
     */
    @GetMapping("/config/name")
    public String config() {
        String name = userProperties.getName();
        if (name == null)
            return "没有从配置中心获取到值";
        return name;
    }

    /**
     * 测试从配置中心获取值
     */
    @GetMapping("/config/name1")
    public String config1() {
        return name;
    }

}
